package br.unb.midialab.evo.osc;

import java.util.logging.Level;
import java.util.logging.Logger;
import netP5.NetAddress;
import oscP5.OscBundle;
import oscP5.OscMessage;
import oscP5.OscP5;

public class OSCMessager {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    OscP5 osc = null;
    private NetAddress myRemoteAddress = null;

    public OSCMessager() {
        this.osc = new OscP5(this, 9999);
    }

    private OscP5 getOsc() {
        if (osc == null) {
            osc = new OscP5(this, 9999);
        }
        return osc;
    }

    void setMyRemoteAddress(NetAddress myRemoteAddress) {
        this.myRemoteAddress = myRemoteAddress;
    }

    NetAddress getMyRemoteAddress() {
        if (myRemoteAddress == null) {
            setMyRemoteAddress(new NetAddress("127.0.0.1", 9998));
        }
        return myRemoteAddress;
    }

    public synchronized void sendTranslationMessage(String fourChars) {
        OscBundle oscB = new OscBundle();
        OscMessage message = new OscMessage("/translation");
        //message.setAddrPattern("/test2");
        message.add(fourChars.toCharArray());
        oscB.add(message);
        //message.add(fourChars);
        //oscB.add(message);
        getOsc().send(oscB, getMyRemoteAddress());
    }
    
    public void sendStringMessage(String addrPattern, String content) {
        OscBundle oscB = new OscBundle();
        OscMessage message = new OscMessage(addrPattern);
//        message.setAddrPattern("/test2");
        System.out.println(content);
        message.add(content);
        oscB.add(message);
        //message.add(fourChars);
        //oscB.add(message);
        getOsc().send(oscB, getMyRemoteAddress());
    }

    public synchronized void sendBangFlangerMessage(String mensagem, int changeFrequency) {

        final String twittie = mensagem;
        final int frequency = changeFrequency;

        Runnable bangThread = new Runnable() {

            public void run() {
                for (int i = 0; i < twittie.length(); i++) {
                    Character c = twittie.charAt(i);
                    //message.setAddrPattern("/test2");
                    if (c.equals('1')) {
                        sendBangMessage(c);
                        try {
                            //System.out.println((c) + " <- Sent to PD");
                            Thread.sleep(frequency / twittie.length());
                        } catch (InterruptedException ex) {
                            Logger.getLogger(OSCMessager.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

            }
        };

        Thread bang = new Thread(bangThread);
        bang.start();
    }

    public synchronized void sendBangMessage(char c) {
        OscBundle oscB = new OscBundle();
        OscMessage message = new OscMessage("/bangflanger");
        Byte b = Byte.valueOf(String.valueOf(c));
        message.add(b);
        oscB.add(message);
        getOsc().send(oscB, getMyRemoteAddress());
    }
}
